package com.lxmes.common.utils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.List;

public class SoapExample {
    /**
     * 将对象转换为XML格式的字符串
     *
     * @param object 要转换的对象
     * @return XML格式的字符串
     * @throws JAXBException 如果发生JAXB转换错误
     */
    public static String convertObjectToXML(Object object) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter sw = new StringWriter();
        marshaller.marshal(object, sw);
        return sw.toString();
    }
    /**
     * 将对象集合转换为XML格式的字符串
     *
     * @param Object 要转换的对象
     * @return XML格式的字符串
     * @throws JAXBException 如果发生JAXB转换错误
     */
    public static String convertObjectListToXML(List<Object> Object) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Object.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter sw = new StringWriter();
        marshaller.marshal(Object, sw);
        return sw.toString();
    }


}
